//
//  TSTabbar.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/7.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

protocol TSTabbarDelegate : NSObjectProtocol {
    func didClickvip()
}

class TSTabbar: UITabBar {
    
    weak var ts_delegate : TSTabbarDelegate?
    var vipBtn : UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        vipBtn = UIButton.init(type: .custom)
        vipBtn.setBackgroundImage(UIImage(named: "tabbar_addVip"), for: .normal)
        vipBtn.addTarget(self, action: #selector(vipAction), for: .touchUpInside)
        vipBtn.frame = CGRect(x: 0, y: 0, width: 45, height: 45)
        self.addSubview(vipBtn)

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func vipAction() {
        if self.ts_delegate != nil {
            self.ts_delegate?.didClickvip()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()

        vipBtn.center = CGPoint(x: SCREEN_WIDTH * 0.5, y: 8);
        
        let buttonY : CGFloat = 8
        let buttonW : CGFloat = SCREEN_WIDTH / 5.0
        let buttonH : CGFloat = 49
        var index = 0
        for item in self.subviews {
            if (!(item is UIControl) || (item == vipBtn)) {
                continue
            }
            let buttonX = buttonW * ((CGFloat(index) > 1) ? (CGFloat(index) + 1) : CGFloat(index))
            item.frame = CGRect(x: buttonX, y: buttonY, width: buttonW, height: buttonH)
            
            index += 1
        }
    }
    
}
